package com.mtiiot.fstcpclient;

import com.mtiiot.fscore.inter1.EmailService1;
import com.mtiiot.fscore.inter1.LogService1;
import com.mtiiot.fscore.inter1.RedisMqService1;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.*;

/**
 * Created by Administrator on 16-5-30.
 */
//@Service("dubboConsumer")
public class DubboConsumer {

    /**
     * 获得指定文件的byte数组
     */
    public static byte[] getBytes(String filePath){
        byte[] buffer = null;
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
            byte[] b = new byte[1000];
            int n;
            while ((n = fis.read(b)) != -1) {
                bos.write(b, 0, n);
            }
            fis.close();
            bos.close();
            buffer = bos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer;
    }


    public static void testLog1(){
        try{
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
            LogService1 rs=context.getBean("logService1", LogService1.class);
            boolean f=rs.sendLog(new MetadataInfo("54e08d066a894a96be9f75854a72e5ca","192.168.2.368","validate","serviceclass","execmethod"),LogLevel.ERROR,"","netty错误!");
            System.out.println(f);
            f=rs.sendLog(new MetadataInfo("54e08d066a894a96be9f75854a72e5ca","192.168.2.368","maintenance","maintenanceclass","execmethod"),LogLevel.LOGIC,"","仪器平台错误!");
            System.out.println(f);
            f=rs.sendLog(new MetadataInfo("54e08d066a894a96be9f75854a72e5ca","192.168.2.368","boxneterror","boxerrorclass","execmethod"),LogLevel.IINFO,"","盒子错误!");
            System.out.println(f);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    public static void testRedisMq1(){
        try{
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
            RedisMqService1 ms=context.getBean("redisMqService", RedisMqService1.class);
            int i=ms.produceMessage(new MetadataInfo("8f1b37d5aa8b45699900d46473d1de44","192.168.2.368","validate","serviceclass","execmethod"),new MqInfo("tp1","key1",new User("lcy","123456")));
            System.out.println(i);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    public static void testEmail(){
        try{
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
            Class c=EmailService1.class;
            EmailService1 emailService1=context.getBean("emailService1", EmailService1.class);
            String[] adds=new String[1];
            adds[0]="417543847@qq.com";
            for(int i=0;i<10;i++){
                emailService1.sendEmailByText(new MetadataInfo("7ffe65b4dc7a489db23c32a4d7b7d2cf"),adds,"测试主题"+i,"测试内容"+i);
                System.out.println("down"+i);
            }

        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    /*public static void testReport(){
        try {
            //仪器日结报告单
            InsDailyReportData rd=new InsDailyReportData();
            rd.insName="SMART3000";
            rd.date="2016-07-25";
            //==================概要数据封装================
            List<Outline> ols=new ArrayList<Outline>();
            Outline ol=new Outline();
            ol.date="2016-07-25";
            ol.testCount=150;
            ol.qcCount=12;
            ol.calibrationCount=12;
            ol.reagent="180";
            ol.workTime="3小时17分钟";
            ols.add(ol);
            ol=new Outline();
            ol.date="2016-07-25";
            ol.testCount=150;
            ol.qcCount=12;
            ol.calibrationCount=12;
            ol.reagent="180";
            ol.workTime="3小时17分钟";
            ols.add(ol);
            rd.ols=ols;

            //==================基础检验数据封装===============
            List<BaseTest> bts=new ArrayList<BaseTest>();
            BaseTest bt=new BaseTest();
            bt.count=12;
            bt.xxxxCode="CGRP1000002007";
            bt.endTime="18:06";
            bt.seq=12;
            bt.startTime="10:14";
            bt.testName="甲种胎儿球蛋白";
            bts.add(bt);
            bt=new BaseTest();
            bt.count=12;
            bt.xxxxCode="CGRP1000002007";
            bt.endTime="18:06";
            bt.seq=12;
            bt.startTime="10:14";
            bt.testName="甲种胎儿球蛋白";
            bts.add(bt);
            rd.bts=bts;

            //=================质控数据封装===================
            List<QcTest> qts=new ArrayList<QcTest>();
            QcTest qt=new QcTest();
            qt.count=12;
            qt.xxxxCode="CGRP1000002007";
            qt.endTime="18:06";
            qt.seq=12;
            qt.startTime="10:14";
            qt.testName="甲种胎儿球蛋白";
            qts.add(qt);
            qt=new QcTest();
            qt.count=12;
            qt.xxxxCode="CGRP1000002007";
            qt.endTime="18:06";
            qt.seq=12;
            qt.startTime="10:14";
            qt.testName="甲种胎儿球蛋白";
            qts.add(qt);
            rd.qts=qts;
            //=================定标数据封装===================
            List<CalibrationTest> cts=new ArrayList<CalibrationTest>();
            CalibrationTest ct=new CalibrationTest();
            ct.seq=1;
            ct.testName="甲种胎儿球蛋白";
            ct.xxxxCode="CGRP1000002007";
            ct.fittingRate="0.87";
            ct.status="通过";
            ct.time="09:15";
            cts.add(ct);
            ct=new CalibrationTest();
            ct.seq=1;
            ct.testName="甲种胎儿球蛋白";
            ct.xxxxCode="CGRP1000002007";
            ct.fittingRate="0.87";
            ct.status="不通过";
            ct.time="09:15";
            cts.add(ct);
            rd.cts=cts;
            //===============试剂数据====================
            List<Reagent> rs=new ArrayList<Reagent>();
            Reagent r=new Reagent();
            r.seq=1;//序号
            r.tn="甲种胎儿球蛋白";//项目名称
            r.rl="160703";//试剂批号
            r.ru="1000";//实际消耗
            r.tc=12;//检验个数
            r.cbCount=8;//定标个数
            r.qcCount=9;//质控个数
            r.dfCount=20;//差异个数
            rs.add(r);
            r=new Reagent();
            r.seq=1;//序号
            r.tn="甲种胎儿球蛋白";//项目名称
            r.rl="160703";//试剂批号
            r.ru="1000";//实际消耗
            r.tc=12;//检验个数
            r.cbCount=8;//定标个数
            r.qcCount=9;//质控个数
            r.dfCount=20;//差异个数
            rs.add(r);
            rd.rs=rs;
            //===================工时统计数据===================
            List<WorkStatistics> wss=new ArrayList<WorkStatistics>();
            WorkStatistics ws=new WorkStatistics();
            ws.pot=400;//开机时间
            ws.wt=200;//工作时间
            ws.pt=50;//暂停时间
            ws.it=150;//空闲时间
            ws.ur="60";//利用率
            wss.add(ws);
            ws=new WorkStatistics();
            ws.pot=400;//开机时间
            ws.wt=200;//工作时间
            ws.pt=50;//暂停时间
            ws.it=150;//空闲时间
            ws.ur="60";//利用率
            wss.add(ws);
            rd.wss=wss;
            //===================日志相关数据数据===================
            List<LogInfo> lis=new ArrayList<LogInfo>();
            LogInfo li=new LogInfo();
            li.time="09:45";//时间
            li.logLevel="错误";//类别
            li.message="AFP试剂不足";//消息
            lis.add(li);
            li=new LogInfo();
            li.time="09:45";//时间
            li.logLevel="错误";//类别
            li.message="AFP试剂不足";//消息
            lis.add(li);
            rd.lis=lis;

            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
            ReportService reportService=context.getBean("reportService", ReportService.class);
            ReportInfo reportInfo=reportService.createReport("TJ_BUSINESS_REPORT_1_20160726172906696_l4y",rd,ReportFormatType.HTML);
            FileUtil.byte2File(reportInfo.getFile(),"E:/report/",reportInfo.getId()+"."+reportInfo.getReportExt());

            Document doc = new Document(new FileInputStream(new File("D:\\devinlu\\项目_平台管理\\仪器日结报告单.docx")));
            ReportingEngine engine = new ReportingEngine();
            engine.buildReport(doc, rd, "rd");
            doc.save(new FileOutputStream(new File("D:\\devinlu\\项目_平台管理\\仪器日结报告单-out.pdf")),40);//生成报告单
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }*/
    public static void main(String[] args) {
        testEmail();
        //testReport();
        //testLog1();
        //testRedisMq1();
        // context.start();
//        DemoService demoService = context.getBean("demoService", DemoService.class);
//        System.out.println(demoService.sayHello("aaa"));

        //RedisService rs=context.getBean("redisService", RedisService.class);
//        try {
//           ObjectMapper mapper = new ObjectMapper();
//            for(int i=0;i<10;i++){
//                Map<String,Object> map=new HashMap<String, Object>();
//                map.put("sysId","sysId_"+i);
//                map.put("sysName","netty");
//                map.put("l1Name","l1Name"+i);
//                map.put("l2Name","l2Name"+i);
//                map.put("l3Name","l3Name"+i);
//                map.put("level","error");
//                map.put("size",2.8*i);
//                map.put("year",2016);
//                map.put("month",7);
//                map.put("day", 25);
//                String json=mapper.writeValueAsString(map);
//                Long l= rs.lpush("rediskey-fs-server-log-metadata",json);
//                System.out.println(l);
//            }
 

//        FastdfsService fs=context.getBean("fastdfsService", FastdfsService.class);
//        ReportService reportService=context.getBean("reportService", ReportService.class);
//        LogService logService=context.getBean("logService",LogService.class);
            /////////////////////////////////////log测试调用////////////////////////////////////////////////////
            //boolean f = logService.sendLog(SysName.SYSNAME_FS, LogLevel.ERROR,"TJ","fastdfs","getFile","0","test data 0","lcy's host");
//            for(int i=0;i<1;i++){
//                boolean f = logService.sendLog(SysName.SYSNAME_OTHER, LogLevel.ERROR,"FS","redis","getId","0","test data 0","lcy's host");
//                System.out.println(f);
//                f = logService.sendLog(SysName.SYSNAME_OTHER, LogLevel.WARNING,"FS","redis","getId","0","test data 0","lcy's host");
//                System.out.println(f);

//                boolean f = logService.sendLog(SysName.SYSNAME_INS, LogLevel.ERROR,"TJ","redis","store","0","test data 0","lcy's host");
//                System.out.println(f);
//                f = logService.sendLog(SysName.SYSNAME_INS, LogLevel.LOGIC,"TJ","redis","store","0","test data 0","lcy's host");
//                System.out.println(f);
//                f = logService.sendLog(SysName.SYSNAME_INS, LogLevel.WARNING,"TJ","redis","store","0","test data 0","lcy's host");
//                System.out.println(f);

//                f = logService.sendLog(SysName.SYSNAME_TJ, LogLevel.ERROR,"TJ","report","createReport","0","test data 0","lcy's host");
//                System.out.println(f);
//                f = logService.sendLog(SysName.SYSNAME_TJ, LogLevel.WARNING,"TJ","report","createReport","0","test data 0","lcy's host");
//                System.out.println(f);

//                f = logService.sendLog(SysName.SYSNAME_INSANALYZE, LogLevel.ERROR,"TJ","report","downLoadTemplate","0","test data 0","lcy's host");
//                System.out.println(f);

//                f = logService.sendLog(SysName.SYSNAME_INSANALYZE, LogLevel.LOGIC,"TJ","report","downLoadTemplate","0","test data 0","lcy's host");
//                System.out.println(f);
//                f = logService.sendLog(SysName.SYSNAME_INSANALYZE, LogLevel.WARNING,"TJ","report","downLoadTemplate","0","test data 0","lcy's host");
//                System.out.println(f);

//                f = logService.sendLog(SysName.SYSNAME_KAFKA, LogLevel.ERROR,"TJ","fastdfs","deleteFile","0","test data 0","lcy's host");
//                System.out.println(f);
//                f = logService.sendLog(SysName.SYSNAME_KAFKA, LogLevel.WARNING,"TJ","fastdfs","deleteFile","0","test data 0","lcy's host");
//                System.out.println(f);

//                f = logService.sendLog(SysName.SYSNAME_FS, LogLevel.ERROR,"TJ","fastdfs","getFile","0","test data 0","lcy's host");
//                System.out.println(f);
//                f = logService.sendLog(SysName.SYSNAME_FS, LogLevel.WARNING,"TJ","fastdfs","getFile","0","test data 0","lcy's host");
//                System.out.println(f);

//                f = logService.sendLog(SysName.SYSNAME_NETTY, LogLevel.ERROR,"TJ","fastdfs","getFile","0","test data 0","lcy's host");
//                System.out.println(f);
//                f = logService.sendLog(SysName.SYSNAME_NETTY, LogLevel.WARNING,"TJ","fastdfs","getFile","0","test data 0","lcy's host");
//                System.out.println(f);
//            }





         /////////////////////////////////////report测试调用////////////////////////////////////////////////////
            //public String uploadTemplate(SysName sysName,String l1Name,String l2Name,String l3Name,String templateAlias,TemplateExtType templateExtType,String templateComment,byte[] file);ok
            //public TemplateInfo downLoadTemplateById(String id);ok
            //public TemplateInfo downLoadTemplateById(String id,ReportFormatType reportFormatType);ok
            //public List<TemplateInfo> downLoadTemplateByIds(List<String> ids);ok
            //public List<TemplateInfo> downLoadTemplateByIds(List<String> ids,ReportFormatType reportFormatType);ok
            //public List<TemplateInfo> downLoadTemplateBySysName(SysName sysName);ok
            //public List<TemplateInfo> downLoadTemplateBySysName(SysName sysName,ReportFormatType reportFormatType);ok
            //public byte[] templateReportPreview(String id,ReportFormatType reportFormatType);//暂时不做
            //public TemplateInfo findTemplateInfoById(String id);ok
            //public List<TemplateInfo> findTemplateInfoByIds(List<String> ids);ok
            //public List<TemplateInfo> findTemplateInfoBySysName(SysName sysName);ok
            //public boolean updateTemplateInfo(SysName sysName,String l1Name,String l2Name,String l3Name,String id,String templateAlias,TemplateExtType templateExtType,String templateComment,byte[] file);ok
            //public boolean delTemplateById(String id);ok
            //public boolean delTemplateByIds(List<String> ids);ok
            //public boolean delTemplateBySysName(SysName sysName);ok
            //public boolean hasTemplate(String id);ok
            //public ReportInfo createReport(String templateId,Object dataSourceBean,ReportFormatType reportFormatType);
            //public ReportInfo downLoadReportById(String id);


//        String filePath="D:/devinlu/项目_体检/报告单模板/report_word/C13报告单  C14Rep2(ok).docx";
//        byte[] file=getBytes(filePath);
//        String templateId=reportService.uploadTemplate(SysName.SYSNAME_TJ,"l1","l2","l3","模板1", TemplateExtType.EXT_DOCX,"模板备注1",file);
//        System.out.println(templateId);
//        templateId=reportService.uploadTemplate(SysName.SYSNAME_TJ,"l1","l2","l3","模板2", TemplateExtType.EXT_DOCX,"模板备注2",file);
//        System.out.println(templateId);
//        templateId=reportService.uploadTemplate(SysName.SYSNAME_YG,"l1","l2","l3","模板3", TemplateExtType.EXT_DOCX,"模板备注3",file);
//        System.out.println(templateId);
//        templateId=reportService.uploadTemplate(SysName.SYSNAME_YG,"l1","l2","l3","模板4", TemplateExtType.EXT_DOCX,"模板备注4",file);
//        System.out.println(templateId);
        //boolean f=reportService.hasTemplate("");ok
        // boolean f= reportService.delTemplateById("SYSNAME_TJ_l1_l2_l3_20160609212225802");
        //TemplateInfo templateInfo=reportService.downLoadTemplateById("TJ_l1_l2_l3_20160609215349421");
//       TemplateInfo templateInfo=reportService.downLoadTemplateById("TJ_l1_l2_l3_20160609215349421", ReportFormatType.JPEG);
//       FileUtil.byte2File(templateInfo.getFile(),"D:/report/",templateInfo.getTemplateAlias()+"."+templateInfo.getTemplateExt());
//         List<String> ids=new ArrayList<String>();
//         ids.add("TJ_l1_l2_l3_20160610142855618");
//         ids.add("YG_l1_l2_l3_20160610142857913");
//         ids.add("YG_l1_l2_l3_20160610142901582");
//         //List<TemplateInfo> templateInfos= reportService.downLoadTemplateByIds(ids,ReportFormatType.PDF);
//         List<TemplateInfo> templateInfos =   reportService.downLoadTemplateBySysName(SysName.SYSNAME_TJ,ReportFormatType.PDF);
         //TemplateInfo templateInfo =  reportService.findTemplateInfoById("TJ_l1_l2_l3_20160609223538982");
         //List<TemplateInfo> templateInfos =   reportService.findTemplateInfoByIds(ids);
      //   List<TemplateInfo> templateInfos=   reportService.findTemplateInfoBySysName(SysName.SYSNAME_FS);
//         String filePath="D:/devinlu/项目_体检/报告单模板/report_word/HPV报告单  HpvLctRep(ok).docx";
//         byte[] file=getBytes(filePath);
//         boolean f = reportService.updateTemplateInfo("TJ_l1_l2_l3_20160610142855618","模板修改2",TemplateExtType.EXT_DOCX,"模板修改备注2",file);
        // boolean f=reportService.delTemplateById("TJ_l1_l2_l3_20160610142855129");
         //boolean f=reportService.delTemplateBySysName(SysName.SYSNAME_YG);
//        ObjectMapper mapper = new ObjectMapper();
//        String json=mapper.writeValueAsString(templateInfos);
//          boolean f=  reportService.delTemplateByIds(ids);
//          boolean f=reportService.hasTemplate("TJ_l1_l2_l3_20160610144241032");
         //ReportInfo reportInfo= reportService.createReport("TJ_l1_l2_l3_20160610144241032", ReportDataUtil.getReportData(),ReportFormatType.PDF);
//         ReportInfo reportInfo = reportService.downLoadReportById("TJ_BUSINESS_REPORT_1_20160615134448181");
//         ObjectMapper mapper = new ObjectMapper();
//         String json=mapper.writeValueAsString(reportInfo);
//         System.out.println(json);
//         FileUtil.byte2File(reportInfo.getFile(),"D:/report/",reportInfo.getId()+"."+reportInfo.getReportExt());
         // System.out.println(templateInfos.size());

            /////////////////////////////////////redis测试调用////////////////////////////////////////////////////
//            for(int i=1;i<=101;i++){
//                String c=rs.getCache("city_11");
//                System.out.println(c);
//                System.out.println("========"+i);
//            }

            //for(int i=0;i<10;i++){
//                boolean f=rs.saveOrUpdateCache(SysName.SYSNAME_TJ,"l1","l2","l3","test_lcy11","hello world1!");
//                System.out.println(f);
            //}
           //boolean f=rs.saveOrUpdateCache(SysName.SYSNAME_TJ,"aadd","aadd","dddd","test_redisKey3","handfdfdfdfrererfhsflkafhdsahfasl");
            //  System.out.println(f);
          //boolean r=rs.deleteCache("test_lcy11");
         //   System.out.println(r);
//            r=rs.deleteCache("");
//            System.out.println(r);
//            r=rs.deleteCache(null);
//            System.out.println(r);
//            r=rs.deleteCache("3333333333333");
            //System.out.println(r);
            //rs.storeCache(SysName.SYSNAME_TJ,"","","","test_redisKey3","hello world dubbo");
            //rs.storeCache(SysName.SYSNAME_TJ,"","","","test_redisKey2","hello world dubbo",30);
            //String f= rs.getCache("test_redisKey3");
            //rs.updateCache("test_redisKey1","helo world dubbo  1111111111");
            //rs.updateCache("test_redisKey1","helo world dubbo  1111111111",30);
            //rs.deleteCache("test_redisKey3");
           // System.out.println(f);
            /////////////////////////////////////fastdfs测试调用////////////////////////////////////////////////////
//            String filePath="c:/lcy1.txt";
//            byte[] file=getBytes(filePath);
//            FileInfo fi=fs.storeFile(SysName.SYSNAME_TJ,"1111","1111","1132","txt",file);
            // boolean f=fs.delFile("");
//            List<String> list=new ArrayList<String>();
//            list.add("TJ_DEF_DEF_DEF_20160606161748860");
//            list.add("TJ_DEF_DEF_DEF_20160606162731446");
//            for(int i=0;i<20;i++){
//                System.out.println("==========="+i+"===========");
//                FileInfo fi=fs.getFile("TJ_BUSINESS_REPORT_1_20160613162537265");
////            List<FileInfo> list1=fs.getFile(list);
//                ObjectMapper mapper = new ObjectMapper();
//                String json=mapper.writeValueAsString(fi);
//                //boolean f=fs.hasFile("33333");
//                //boolean f=rs.hasKey("test_redisKey3");
//                // boolean f=report.hasTemplate("333");
////          boolean f=fs.delFiles(list);
//                System.out.println(json);
//            }

//            System.out.println(report.hasTemplate("3333"));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}
